package com.rds.workflow.controller;

import com.rds.common.core.controller.BaseController;
import com.rds.common.core.domain.AjaxResult;
import com.rds.common.core.page.TableDataInfo;
import com.rds.common.utils.DateUtils;
import com.rds.common.utils.SecurityUtils;
import com.rds.common.utils.uuid.SnowFlakeUtils;
import com.rds.workflow.domain.RdaWfUserAgent;
import com.rds.workflow.service.IRdaWfUserAgentService;
import com.rds.workflow.utils.WorkflowStateid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pms/workflowAgent")
public class RdaWfUserAgentController extends BaseController {

    @Autowired
    private IRdaWfUserAgentService rdaWfUserAgentService;

    /**
     * 查询全局委托
     */
    @PreAuthorize("@ss.hasPermi('pms:workflowAgent:agentList')")
    @GetMapping("/agentList")
    public TableDataInfo agentList(RdaWfUserAgent rdaWfUserAgent){
        startPage();
        rdaWfUserAgentService.updateRdaWfUserAgentByStateid(WorkflowStateid.agentObsoletedid,WorkflowStateid.agentCreateid,WorkflowStateid.agentActivationid);
        return getDataTable(rdaWfUserAgentService.selectRdaWfUserAgentList(rdaWfUserAgent));
    }

    /**
     * 新增全局委托
     */
    @PreAuthorize("@ss.hasPermi('pms:workflowAgent:addWorkflowAgent')")
    @PostMapping("/addWorkflowAgent")
    @Transactional
    public AjaxResult addWorkflowAgent(@RequestBody RdaWfUserAgent rdaWfUserAgent){
        rdaWfUserAgent.setId(SnowFlakeUtils.nextId());
        rdaWfUserAgent.setOwnerid(SecurityUtils.getUserId());
        rdaWfUserAgent.setTypeid(WorkflowStateid.agentTypeid);
        rdaWfUserAgent.setPolicyid(WorkflowStateid.agentPolicyid);
        rdaWfUserAgent.setStateid(WorkflowStateid.agentCreateid);
        rdaWfUserAgent.setCreateTime(DateUtils.getNowDate());
        return toAjax(rdaWfUserAgentService.insertRdaWfUserAgent(rdaWfUserAgent));
    }

    /**
     * 删除全局委托
     */
    @PreAuthorize("@ss.hasPermi('pms:workflowAgent:deleteWorkflowAgent')")
    @DeleteMapping("/deleteWorkflowAgent/{ids}")
    @Transactional
    public AjaxResult deleteWorkflowAgent(@PathVariable Long[] ids){
        return toAjax(rdaWfUserAgentService.deleteRdaWfUserAgentByIds(ids));
    }

    /**
     * 查询全局委托详情
     */
    @PreAuthorize("@ss.hasPermi('pms:workflowAgent:agentList')")
    @GetMapping("/agentList/{id}")
    public AjaxResult agentList(@PathVariable("id") Long id){
        return AjaxResult.success(rdaWfUserAgentService.selectRdaWfUserAgentByid(id));
    }

    /**
     * 修改全局委托
     */
    @PreAuthorize("@ss.hasPermi('pms:workflowAgent:agentList')")
    @PutMapping("/agentList")
    @Transactional
    public AjaxResult updateAgent(@RequestBody RdaWfUserAgent rdaWfUserAgent){
        rdaWfUserAgent.setModifyTime(DateUtils.getNowDate());
        return toAjax(rdaWfUserAgentService.updateRdaWfUserAgent(rdaWfUserAgent));
    }

}
